﻿@inject IHttpService _httpService
@inject IDataCacheService _dataCacheService
@inject IPopupService PopupService


<div class="row">
    @if (_dataCacheService.IsApp)
    {
        <div class="col-12">
            <MDCard Class="mb-4">
                <MTabs @bind-Value="selected" IconsAndText Grow CenterActive>

                 <MTab Value="0">
                        全部<MIcon>mdi-comment-multiple-outline</MIcon>
                 </MTab>
                 <MTab Value="1">
                     回复我的<MIcon>mdi-comment-text-outline</MIcon>
                 </MTab>
                 <MTab Value="2">
                     编辑通过提醒<MIcon>mdi-checkbox-marked-circle-outline</MIcon>
                 </MTab>
                 <MTab Value="3">
                     编辑驳回提醒<MIcon>mdi-alert-circle-outline</MIcon>
                 </MTab>
             </MTabs>
         </MDCard>

     </div>
    }
    else
    {
        <div class="col-12 col-lg-3 mb-4">
            <MDCard Class="pa-4">
                <MList Class="pa-0">
                    <MListItemGroup @bind-Value="selected" Color="primary">
                        <MListItem @key="0">
                            <MListItemIcon>
                                <MIcon>mdi-comment-multiple-outline </MIcon>
                            </MListItemIcon>
                            <MListItemContent>
                                <MListItemTitle>全部</MListItemTitle>
                            </MListItemContent>
                        </MListItem>
                         <MListItem @key="1">
                            <MListItemIcon>
                                <MIcon>mdi-comment-text-outline </MIcon>
                            </MListItemIcon>
                            <MListItemContent>
                                <MListItemTitle>回复我的</MListItemTitle>
                            </MListItemContent>
                        </MListItem>
                        <MListItem @key="2">
                            <MListItemIcon>
                                <MIcon>mdi-checkbox-marked-circle-outline</MIcon>
                            </MListItemIcon>
                            <MListItemContent>
                                <MListItemTitle>编辑通过提醒</MListItemTitle>
                            </MListItemContent>
                        </MListItem>
                        <MListItem @key="3">
                            <MListItemIcon>
                                <MIcon>mdi-alert-circle-outline</MIcon>
                            </MListItemIcon>
                            <MListItemContent>
                                <MListItemTitle>编辑驳回提醒</MListItemTitle>
                            </MListItemContent>
                        </MListItem>
                    </MListItemGroup>
                </MList>
            </MDCard>

        </div>
    }

    <div class="col-12 col-lg-9">
        @if (isReady)
        {
            <MWindow @bind-Value="selected">
                  <MWindowItem Value="0">
                    <ScaleTransition>
                        <Element Show="selected==0">
                            <MDCard Style=" display: contents; ">
                                <GroupMessageViewTip Model="Model" />
                            </MDCard>
                        </Element>
                    </ScaleTransition>
                </MWindowItem>
                <MWindowItem Value="1">
                    <ScaleTransition>
                        <Element Show="selected==0">
                            <MDCard Style=" display: contents; ">
                                <GroupMessageViewTip Model="RelyItems" />
                            </MDCard>
                        </Element>
                    </ScaleTransition>
                </MWindowItem>

                <MWindowItem Value="2">
                    <ScaleTransition>
                        <Element Show="selected==1">
                            <MDCard Style=" display: contents; ">
                                <GroupMessageViewTip Model="PassedItems" />
                            </MDCard>
                        </Element>
                    </ScaleTransition>
                </MWindowItem>

                <MWindowItem Value="3">
                    <ScaleTransition>
                        <Element Show="selected==2">
                            <MDCard Style=" display: contents; ">
                                <GroupMessageViewTip Model="UnPassedItems" />
                            </MDCard>
                        </Element>
                    </ScaleTransition>
                </MWindowItem>
            </MWindow>
        }
        else
        {
            <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill />

        }

    </div>

</div>


@code {
    bool isReady = false;
    StringNumber selected = 0;

    public List<CnGalWebSite.DataModel.Model.Message> Model { get; set; } = new List<DataModel.Model.Message>();
    public List<CnGalWebSite.DataModel.Model.Message> RelyItems { get; set; } = new List<DataModel.Model.Message>();
    public List<CnGalWebSite.DataModel.Model.Message> PassedItems { get; set; } = new List<DataModel.Model.Message>();
    public List<CnGalWebSite.DataModel.Model.Message> UnPassedItems { get; set; } = new List<DataModel.Model.Message>();
    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }
    protected override async Task OnInitializedAsync()
    {
        try 
        {
            //获取文章信息
            Model = await _httpService.GetAsync<List<CnGalWebSite.DataModel.Model.Message>>("api/space/GetUserMessages");

            Model = Model.OrderByDescending(s => s.PostTime).ToList();

            RelyItems = Model.Where(s => s.Type == MessageType.ArticleReply || s.Type == MessageType.SpaceReply || s.Type == MessageType.CommentReply).ToList();
            PassedItems = Model.Where(s => s.Type == MessageType.ExaminePassed).ToList();
            UnPassedItems = Model.Where(s => s.Type == MessageType.ExamineUnPassed).ToList();

            if (PassedItems.Any(s => s.IsReaded == false))
            {
                selected = 1;
            }
            if (RelyItems.Any(s => s.IsReaded == false))
            {
                selected = 0;
            }
            if (UnPassedItems.Any(s => s.IsReaded == false))
            {
                selected = 2;
            }

            isReady = true;
            StateHasChanged();
            await OnReadedAllMessage();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取用户消息列表失败");
        }
    }

    public async Task OnReadedAllMessage()
    {
        try
        {
            var obj = await _httpService.GetAsync<Result>("api/space/ReadedAllMessages/");
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("使消息已读失败", obj.Error);
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "设置消息已读失败");
        }
    }
}
